var jqTreeTypeOption = function(){
	this.callType = new Array("D", "P", "J");
	this.returnJson = new Array("jsonFile","jsonManual", "jsonDb");
	this.returnBack = new Array("htmlFile","jsonFile","htmlManual","jsonManual", "htmlDb", "jsonDb");
};

var jqTreeview = function(options){
	this.xWindow = new Browser();
	this.jqTreeOpts = options;
};

jqTreeview.prototype.listInit = function(obj){
	return this.listSetAtBrowser(obj);
};

jqTreeview.prototype.listSetAtBrowser = function(obj){
	var opts = this.jqTreeOpts;
	var collapsed = (opts.collapsed != null && opts.collapsed != "undefined") ? opts.collapsed : false;
	var rootKey = opts.jsonRootKey;
	var rootTreeId = opts.rootTreeId;
	var treeColourClass = opts.treeColourClass;
	var canvas = opts.canvas;
	var errMsg = (opts.errMsg != null && opts.errMsg != "undefined") ? opts.errMsg : "Data Error";
	var rootUlTag = $("<ul id='"+rootTreeId+"' class='"+treeColourClass+"' />");
	
	if(this.xWindow.isIE && this.xWindow.version < 9.0){
		//Browser compatibility: All browser.
		for(key in obj) {
			if(key == rootKey){
				if(obj.hasOwnProperty(key)) {					
					for(var o in obj[key]){
						if(obj[key].hasOwnProperty(o)) {
							var item = obj[key][o];	
							
							if(item.fnUseYN == "Y"){
								var urlTag = (item.fnServletUrl != "undefined" && item.fnServletUrl != null && item.fnServletUrl.replace(/ /g, "")) ? 
										"<span class='noneSpan'>"+item.fnServletUrl+"</span>" : "";
								var classTag = (collapsed) ? " class='open'" : " class='closed'";
								classTag = (item.fnCollapse) ? " class='open'" : " class='closed'";
								
								var fImg = item.fileImgPath;
								fImg = (fImg != null && fImg != "undefined" && fImg != "") ? "<img src='"+fImg+"' />": "";
								
								var liTag = "<li "+classTag+" id='"+item.fnId+"'>"+fImg+"<span>"+item.fnAlias+"</span>"+urlTag+"</li>";
								if(item.fnParentId == key || item.fnParentId == "0"){
									$(rootUlTag).append($(liTag));
								}else{
									var target = $(rootUlTag).find("#"+item.fnParentId);
									$(target).not(":has(> ul)").append("<ul />");
									$(target).find("> ul").append($(liTag));
								}
							}
						}
					}
				}else{
					var liTag = "<li id='0'><span>Empty</span><span class='noneSpan'></span></li>";
					$(rootUlTag).append($(liTag));
				}
			}else{ 
				if(key == "Error"){ 
					var liTag = "<li id='0'><span>Empty</span><span class='noneSpan'></span></li>";
					$(rootUlTag).append($(liTag));
					
					$(canvas).html(errMsg);
				}
			}
		}
		
	}else{
		//Browser compatibility: Firefox(Gecko)4 | Chrome()2.0) | Internet Explorer9 | Opera12 |  Safari5 
		//Specifications: ECMAScript Language Specification 5.1th Edition (ECMA-262) - Implemented in JavaScript 1.8.5
		Object.keys(obj).forEach(function(key){
			if(key == rootKey){
				if(obj[key].length){ 
					obj[key].forEach(function(item, idx){
						if(item.fnUseYN == "Y"){
							var urlTag = (item.fnServletUrl != "undefined" && item.fnServletUrl != null && item.fnServletUrl.replace(/ /g, "")) ? 
									"<span class='noneSpan'>"+item.fnServletUrl+"</span>" : "";
							var classTag = (collapsed) ? " class='open'" : " class='closed'";
							classTag = (item.fnCollapse) ? " class='open'" : " class='closed'";

							var fImg = item.fileImgPath;
							//fImg = "<img src='"+fImg+"' />": "";
							var fImgStyle = (fImg != null && fImg != "undefined" && fImg != "") ? 
									"style='background: url("+fImg+") 0px 0px no-repeat; padding-left:18px;'" : "";
							
							var liTag = "<li "+classTag+" id='"+item.fnId+"'><span "+fImgStyle+">"+item.fnAlias+"</span>"+urlTag+"</li>";
							if(item.fnParentId == key || item.fnParentId == "0"){
								$(rootUlTag).append($(liTag));
							}else{
								var target = $(rootUlTag).find("#"+item.fnParentId);
								$(target).not(":has(> ul)").append("<ul />");
								$(target).find("> ul").append($(liTag));
							}
						}
					});
				}else{
					var liTag = "<li id='0'><span>Empty</span><span class='noneSpan'></span></li>";
					$(rootUlTag).append($(liTag));
				}
			}else{ 
				if(key == "Error"){ 
					var liTag = "<li id='0'><span>Empty</span><span class='noneSpan'></span></li>";
					$(rootUlTag).append($(liTag));
					
					$(canvas).html(errMsg);
				}
			}
		});
	}
	
	return $(rootUlTag);
};

jqTreeview.prototype.treeMenuSet = function(){
	var opts = this.jqTreeOpts;
	var clickingSelected = opts["clickingSelected"];
	var tree = $("#"+opts.rootTreeId).treeview({
		animated: (opts.animated == "undefined" || !opts.animated)?"":opts.animated
		, collapsed: opts.collapsed
		, control: opts.controller
		, persist: (opts.persist == "undefined" || !opts.persist)?"":opts.persist
		, cookieId: (opts.cookieId == "undefined" || !opts.cookieId)?"":opts.cookieId
	});
	var ulActive = (opts.ulActive != null && opts.ulActive != "undefined")?opts.ulActive:false;
	var prevObj = null;
	
	$(tree).find("li").each(function(event){
		var obj = $(this);
		if(ulActive) obj = $(obj).not(":has(> ul)");
		obj = $(obj).find("> span");		
		
		if($(obj).length > 1){
			var li = $(this);
			var titleSpan = $(obj).eq(0);
			var urlSpan = $(obj).eq(1);
			var url = $(urlSpan).text().replace(/ /g, "");

			$(titleSpan).hoverClass();
			$(titleSpan).on("click", function(){
				if(url == "") {
					//alert("Not ready yet!"); 
					return;
				}
				
				if(clickingSelected == null || clickingSelected == "undefined"){
					clickingSelected = true;
				}
				if(!clickingSelected && $(prevObj).is($(li))) return;
				//alert(event);

				if(opts.navi){
					$(prevObj).parents("li").each(function(){
						$(this).find("> span").eq(0).removeClass("menuClickedPerent").removeClass("menuClickedHover");
					});
					$(li).parents("li").each(function(){
						$(this).find("> span").eq(0).addClass("menuClickedPerent").hoverClass("menuClickedHover");
					});
				}

				if($(prevObj).length > 0){ 
					$(prevObj).find("> span").eq(0).removeClass("menuClicked").hoverClass(); 
				}
				$(titleSpan).removeClass().addClass("menuClicked");
				
				var options = new Object();
				options["url"] = url;
				options["type"] = "POST";
				options["dataType"] = "html";
				options["data"] = "treeItemId=" + $(this).parent("li").attr("id");
				options["rtnBoard"] = opts.canvas;
				
				loadTreeviewCanvas(options);
				prevObj = $(li);
			});
		}
	});
};

jqTreeview.prototype.compareArray = function(src, tar){
	var rtnValue = false;

	if(this.xWindow.isIE && this.xWindow.version < 9.0){
		for(var i in src){
			if(src[i].indexOf(tar) != -1){
				rtnValue = true;
				break;
			}
		}
	}else{
		if(src.indexOf(tar) != -1){
			rtnValue = true;
		}
	}

	return rtnValue;
};

var loadTreeviewCanvas = function (opts) {
	var ajax = new jqMyAjax();
	var rtnBoard = opts["rtnBoard"];
	var data = opts["data"];
	
	var option = new Array();
	option["type"] = opts["type"];
	option["url"] = opts["url"];
	option["dataType"] = opts["dataType"];
	option["data"] = (data != null && data.indexOf("frm:") != -1)? $("#"+data.split("frm:")[1]).serialize() : data;
	option["success"] = function(result){
		$(rtnBoard).html(result);
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
		
		var style = "style='font-weight:bold; font-size:14pt;'";
		$(rtnBoard).html("<font "+style+">HTTP STATUS "+xhr.status + "  " + status.toUpperCase()+"</font>");
	};
	
	ajax.callAjax(option);
};